package com.classic.common.simple;

import com.classic.common.MultipleStatusView;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

public class CustomActivity extends AbsActivity {
    private final DependentLayout.LayoutConfig mLayoutParams = new DependentLayout.LayoutConfig(
            DependentLayout.LayoutConfig.MATCH_CONTENT, DependentLayout.LayoutConfig.MATCH_CONTENT);
    private Component mEmptyView;
    private Component mLoadingView;
    private Component mErrorView;
    private Component mNoNetworkView;

    @Override
    void initView() {
        setUIContent(ResourceTable.Layout_activity_custom);
        mMultipleStatusView = (MultipleStatusView) findComponentById(ResourceTable.Id_custom_multiple_status_view);
        mMultipleStatusView.setOnRetryClickListener(mRetryClickListener);
        mLayoutParams.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_fab_loading:
                if (null == mLoadingView) {
                    mLoadingView = createCustomView("自定义加载中视图");
                }
                mMultipleStatusView.showLoading(mLoadingView, mLayoutParams);
                break;
            case ResourceTable.Id_fab_empty:
                if (null == mEmptyView) {
                    mEmptyView = createCustomView("自定义空视图");
                }
                mMultipleStatusView.setEmptyResId(ResourceTable.Id_empty_retry_view);
                mMultipleStatusView.showEmpty(mEmptyView, mLayoutParams);
                break;
            case ResourceTable.Id_fab_error:
                if (null == mErrorView) {
                    mErrorView = createCustomView("自定义错误视图");
                }
                mMultipleStatusView.setErrorResId(ResourceTable.Id_error_retry_view);
                mMultipleStatusView.showError(mErrorView, mLayoutParams);
                break;
            case ResourceTable.Id_fab_no_network:
                if (null == mNoNetworkView) {
                    mNoNetworkView = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_no_network, null, false);
                }
                mMultipleStatusView.setNetWorkResId(ResourceTable.Id_no_network_retry_view);
                mMultipleStatusView.showNoNetwork(mNoNetworkView, mLayoutParams);
                break;
            case ResourceTable.Id_fab_content:
                mMultipleStatusView.showContent(ResourceTable.Layout_custom_content_view, mLayoutParams);
                break;
        }
        mFloatingActionMenu.toggle(false);
    }

    private Text createCustomView(String text) {
        Text tv = new Text(getApplicationContext());
        tv.setId(Utils.generateViewId());
        tv.setText(text);
        tv.setTextSize(16, Text.TextSizeType.FP);
        tv.setTextAlignment(TextAlignment.CENTER);
        tv.setTextColor(new Color(getContext().getColor(ResourceTable.Color_color_item_intro)));
        return tv;
    }
}
